﻿using Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinCourseControls
{
    public partial class FrmListBox : Form
    {
        public FrmListBox()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 添加项按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //添加项   
            lbList01.Items.Add("主任"); // 添加
            lbList01.Items.Insert(2, "年级主任");  // 插入
            lbList01.Items.AddRange(new object[] { 123, 123.45, "工作人员" }); // 添加集合

            int index = lbList01.Items.IndexOf(123);//获取项索引

        }

        /// <summary>
        /// 移除项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemove_Click(object sender, EventArgs e)
        {
            lbList01.Items.Remove(123);//移除 通过项移除
            lbList01.Items.RemoveAt(4);//通过指定索引移除

            //DataSource  指定的数据源中项，不能直接Items.Remove  Add   移除、添加项
        }

        private void FrmListBox_Load(object sender, EventArgs e)
        {
            string sql = "Select BShelfId,BShelfName from BookShelfInfos";
            DataTable dt = SqlHelper.GetDataTable(sql, 1);
            lbShelfList.DisplayMember = "BShelfName";
            lbShelfList.ValueMember = "BShelfId";
            lbShelfList.DataSource = dt;
        }

        /// <summary>
        /// 选中项集合
        /// </summary>
        List<string> listSels = new List<string>();
        /// <summary>
        /// 属性更改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbShelfList_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (var item in lbShelfList.SelectedItems)
            {   // 循环选中项集合
                string val = (item as DataRowView).Row["BShelfName"].ToString();
                if (!listSels.Contains(val))
                    listSels.Add(val);
            }
            foreach (int item in lbShelfList.SelectedIndices)
            {  // 循环选中索引集合
                string val = (lbShelfList.Items[item] as DataRowView).Row["BShelfName"].ToString();
                if (!listSels.Contains(val))
                    listSels.Add(val);
            }
            txtVal.Text = string.Join(",", listSels);
        }

        /// <summary>
        /// 确定按钮-获取选中项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            listSels = new List<string>();
            foreach (var item in lbShelfList.SelectedItems)
            {   // 循环选中项集合
                string val = (item as DataRowView).Row["BShelfName"].ToString();
                if (!listSels.Contains(val))
                    listSels.Add(val);
            }
            //foreach (int item in lbShelfList.SelectedIndices)
            //{   // 循环选中索引集合
            //    string val = (lbShelfList.Items[item] as DataRowView).Row["BShelfName"].ToString();
            //    listSels.Add(val);
            //}
            txtVal.Text = string.Join(",", listSels);
        }

        /// <summary>
        /// 右移按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRight_Click(object sender, EventArgs e)
        {
            if( lbList01.SelectedItems!=null)
            {
                var list = lbList01.SelectedItems;
                foreach (var item in list)
                {
                    lbList02.Items.Add(item);
                }
                foreach(var item in lbList02.Items)
                {
                    lbList01.Items.Remove(item);
                }
            }
        }

        /// <summary>
        /// 左移按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLeft_Click(object sender, EventArgs e)
        {
            if (lbList02.SelectedItems != null)
            {
                var list = lbList02.SelectedItems;
                foreach (var item in list)
                {
                    lbList01.Items.Add(item);
                }
                foreach (var item in lbList01.Items)
                {
                    lbList02.Items.Remove(item);
                }
            }
        }

        /// <summary>
        /// 右全部移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRightAll_Click(object sender, EventArgs e)
        {
            lbList02.Items.AddRange(lbList01.Items);
            lbList01.Items.Clear();
        }

        /// <summary>
        /// 左全部移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLeftAll_Click(object sender, EventArgs e)
        {
            lbList01.Items.AddRange(lbList02.Items);
            lbList02.Items.Clear();
        }
    }
}
